package dayPractice;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Summerday
 */
public class Main122T糖果传递 {

    static int n;
    static long[] s;
    static long[] cnt;

    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        s = new long[n + 1];
        cnt = new long[n + 1];
        for(int i = 1; i <= n; i ++){
            s[i] = sc.nextLong();
            s[i] += s[i - 1];
        }

        long avg = s[n] / n;
        int k = 0;
        for(int i = 1; i < n; i ++) cnt[k ++] = i * avg - s[i];
        cnt[k] = 0;
        Arrays.sort(cnt);
        long res = 0;
        long mid = cnt[n / 2];
        for(int i = 1; i <= n; i ++){
            res += Math.abs(cnt[i] - mid);
        }
        System.out.println(res);
    }

}
